Skip to content

fix(tag-tree): remeasure virtualizer before scrollToIndex on accordion expand#183

Closed
bk-ty wants to merge 1 commit intokenforthewin:mainfrom
bk-ty:fix/tag-tree-virtualizer-scroll
Closed

fix(tag-tree): remeasure virtualizer before scrollToIndex on accordion expand#183
bk-ty wants to merge 1 commit intokenforthewin:mainfrom
bk-ty:fix/tag-tree-virtualizer-scroll

Conversation

@bk-ty
Copy link
Copy Markdown
Contributor

@bk-ty bk-ty commented May 2, 2026

Fix: tag-tree accordion scrolls to wrong row after expand

Problem

Clicking a tag category in the left-panel accordion scrolls to the correct row only on the second click. On the first click, the virtualizer measures against its pre-expand layout, so the computed offset lands in the wrong place.

Root cause

@tanstack/react-virtual caches element heights. When an accordion expands, the list gains rows, but the virtualizer doesn't re-measure until its next render pass. scrollToIndex runs against stale offsets and overshoots/undershoots.

Fix

Call virtualizer.measure() before scrollToIndex, on the tagIndexMap built from the freshly-expanded state. One extra measurement, no behavior change in the non-accordion path.

Testing

  • Manual: expand each accordion section, verified scroll lands on the selected tag on first click, at the top-of-viewport threshold used by the existing code.
  • npx vitest run — 115/115 pass (no regression in existing tag-tree tests).

Files

  • src/components/TagTreePanel.tsx (+17 / −6)

Two issues with the tag tree virtualizer after expand/collapse:

1. Expand/collapse changed flatTags length but the virtualizer did not
   remeasure, causing the scroll area to show the wrong total height
   until the next unrelated re-render. Added a useEffect on flatTags
   and virtualizer that calls virtualizer.measure() when the tree
   structure changes.

2. The scroll-to-selected effect read tagIndexMap via the closure's
   stale capture rather than the latest value at fire time, so the
   first render after a tag became selected would scroll to index 0
   instead of the target tag. Added tagIndexMapRef (updated on every
   render) so the effect always reads the current map without needing
   tagIndexMap as a dependency (which would create spurious re-fires).
@bk-ty bk-ty closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant